/**
 * 全局project_key验证,是否传递
 * @param {object} app koa 实例
 * 
 */

module.exports = (app) => {
    return async (ctx, next) => {

        // 只对/api校验
        if (ctx.path.indexOf('/api') < 0) {
            return await next();
        }

        // 白名单过滤(这些不需要project_key)
        const path = ['/api/project', '/api/project/list', '/api/project/model_list']

        if (path.includes(ctx.path)) {
            return await next();
        }

        let { project_key } = ctx.headers;

        if (!project_key) {
            const resBody = {
                success: false,
                code: 446,
                msg: 'project_key is not found！'
            }
            ctx.status = 200;
            ctx.body = resBody;
            return;

        }

        ctx.project_key = project_key;

        await next();
    }
}